package com.cloud.rocketmqtxconsumer.config;

import com.cloud.rocketmqtxconsumer.listener.DLQOrderMessageListener;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.annotation.PostConstruct;

@Configuration
@SuppressWarnings("all")
public class RocketDLQConsumerConfig {

    private static final String DLQ_CONSUMER_GROUP = "dlq_consumer_transaction_group";
    private static final String NAME_SERVER_ADDRESS="192.168.142.142:9876";
    //监听消息处理的死信队列，死信队列的TOPIC规则是 %DLQ% + 原 group 名称
    private static final String DLQ_MESSAGE_TOPIC="%DLQ%consumer_transaction_group";

    private DefaultMQPushConsumer pushConsumer;

    @Autowired
    @Qualifier("delMessageListener")
    private DLQOrderMessageListener messageListener;

    @PostConstruct
    public void init() throws MQClientException {
        pushConsumer = new DefaultMQPushConsumer(DLQ_CONSUMER_GROUP);
        pushConsumer.subscribe(DLQ_MESSAGE_TOPIC,"*");
        pushConsumer.setNamesrvAddr(NAME_SERVER_ADDRESS);
        pushConsumer.setMessageListener(messageListener);
        pushConsumer.setMaxReconsumeTimes(2);
        pushConsumer.start();

    }
}
